//
//  ByContentCell.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/19.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByContentCell.h"

@implementation ByContentCell

+ (instancetype)createCellInTableview:(UITableView *)table
                             cellType:(ByContentCellType)cellType {
    
    static NSString *cellIdentifier = @"ByContentCell";
    ByContentCell *cell = [table dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.cellType = cellType;
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style
              reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor whiteColor];
        [self initSubviews];
    }
    return self;
}

- (void)setCellType:(ByContentCellType)cellType {
    
    _cellType = cellType;
    [self setNeedsLayout];
}

- (void)setModel:(ByNetNewsItemModel *)model {
    _model = model;
    [self setNeedsLayout];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.iconView setImageWithURL:[NSURL URLWithString:self.model.main_img] placeholderImage:[UIImage imageNamed:@"defaultImage2"]];
    self.contentLabel.text = self.model.post_title;
    self.dateLabel.text = self.model.post_date;
    self.fromLabel.text = [NSString stringWithFormat:@"来源 %@", self.model.post_author_name];
    
    switch (_cellType) {
        case ByContentCellTypeDefautl:
            /** no code */
            break;
        case ByContentCellTypeValue1: {
            self.fromLabel.hidden = NO;
        }
            break;
        case ByContentCellTypeValue2: {
            self.fromLabel.hidden = NO;
            self.detailButton.hidden = NO;
            
            self.contentLabel.width = self.detailButton.left - 20;
            self.fromLabel.width = self.detailButton.left - 10 - self.dateLabel.right;
        }
            break;
        case ByContentCellTypeValue3: {
            self.iconView.hidden = NO;
            
            self.contentLabel.left = self.iconView.right + 10;
            self.contentLabel.width = SCREEN_WIDTH - 10 - self.contentLabel.left;
            self.dateLabel.left = self.contentLabel.left;
            self.dateLabel.width = SCREEN_WIDTH - 10 - self.dateLabel.left;
        }
        default:
            break;
    }

}


- (void)initSubviews {
    
    /** 图片 */
    self.iconView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 70, 35)];
    self.iconView.image = [UIImage imageNamed:@"defaultImage2"];
    [self.contentView addSubview:self.iconView];
    self.iconView.hidden = YES;

    /** 内容Label */
    self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, 15)];
    self.contentLabel.textColor = [UIColor darkGrayColor];
    self.contentLabel.font = FONT(14);
    self.contentLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:self.contentLabel];
    self.contentLabel.text = @"这里放内容";
    
    /** 时间Label */
    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.contentLabel.left, self.contentLabel.bottom + 5, SCREEN_WIDTH / 2, 15)];
    self.dateLabel.textColor = [UIColor lightGrayColor];
    self.dateLabel.font = FONT(12);
    self.dateLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:self.dateLabel];
    self.dateLabel.text = @"2016-07-06 09:13";
    
    /** 来源Label */
    self.fromLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.dateLabel.right, self.dateLabel.top, SCREEN_WIDTH - 10 - self.dateLabel.right, 15)];
    self.fromLabel.textColor = [UIColor lightGrayColor];
    self.fromLabel.font = FONT(12);
    self.fromLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:self.fromLabel];
    self.fromLabel.text = @"来源 工商系";
    self.fromLabel.hidden = YES;
    
    /** 详情按钮 */
    self.detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.detailButton.frame = CGRectMake(SCREEN_WIDTH - 10 - 30, (55 - 20) / 2, 30, 20);
    [self.detailButton setTitle:@"详情" forState:UIControlStateNormal];
    [self.detailButton setTitleColor:JM_THEME_COLOR forState:UIControlStateNormal];
    self.detailButton.titleLabel.font = FONT(14);
    self.detailButton.layer.borderColor = JM_THEME_COLOR.CGColor;
    self.detailButton.layer.borderWidth = 1;
    self.detailButton.layer.cornerRadius = 2;
    [self.contentView addSubview:self.detailButton];
    self.detailButton.hidden = YES;
    
}



@end
